﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Engine.RuleEditor
{
    public partial class ControlHeader : UserControl
    {
        #region Form Dragging API Support
        //The SendMessage function sends a message to a window or windows.

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]

        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        //ReleaseCapture releases a mouse capture

        [DllImportAttribute("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]

        public static extern bool ReleaseCapture();

        #endregion

        public ControlHeader()
        {
            InitializeComponent();
            if (ParentForm != null)
            {
                Title = ParentForm.Text;
            }
        }

        public string Title
        {
            get
            {
                return buttonTitle.Text;
            }
            set
            {
                buttonTitle.Text = value;
            }
        }

        public Image Image
        {
            get
            {
                return buttonTitle.Image;
            }
            set
            {
                buttonTitle.Image = value;
            }
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            if (FormQuestion.Show(buttonClose) == DialogResult.OK)
            {
                ParentForm.Close();
            }
        }

        private void buttonTitle_MouseDown(object sender, MouseEventArgs e)
        {
            // drag the form without the caption bar
            // present on left mouse button
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(ParentForm.Handle, 0xa1, 0x2, 0);
            }
        }
    }
}
